<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Campaigndetail_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	
	function Campaigndetail_model()
	{
		parent::Model();
        $host = $this->db->hostname;
		$arrHost = explode(':',$host);
		$temp =$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
    
    function selfservingGetAllCampaignInfo($campaignid)
    {
        $data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_campaign_info('$campaignid');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
            $row['activate']=date('d/m/Y',strtotime($row['activate']));
            $row['expire']=date('d/m/Y',strtotime($row['expire']));
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfservingGetAllByCampaign($campaignid,$fromdate,$todate,$page,$rownum,$status,$locationid, $status2)
    {
        $data = array();
        $data['banner']=array();
        $data['total']=array();
        $data['totalpage']=array();
        $data['totalprice']=0;
        $total=0;
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_all_by_campaign('$campaignid', '$fromdate', '$todate','$page','$rownum','$status','$locationid','$status2');") or die(mysqli_error());
        
        $result = mysqli_store_result($iconn);
        
        $CI = &get_instance();
        $CI->load->library('mycommon');
        
        while($row = mysqli_fetch_array($result))
		{
            $row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
		 	$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
            switch($row['typead'])
			{
				case '0':
					$row['typead'] = 'CPM';
					break;
                case '1':
					$row['typead'] = 'CPC';
					break;
				default:
					break;
			}
            $row['activate']=date('d/m/Y',strtotime($row['activate']));
            $row['expire']=date('d/m/Y',strtotime($row['expire']));
			$data['banner'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row2 = mysqli_fetch_array($result))
		{
			$data['totalpage'] = $row2['totalpage'];
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
            if($row1['impressions']!='N/A')
            {
                $row1['impressions']=number_format($row1['impressions']);
            }
            if($row1['uviews']!='N/A')
            {
                $row1['uviews']=number_format($row1['uviews']);
            }
			$data['total'][] = $row1;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
	function selfserving_get_list_location()
    {
        $data = array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_list_location();") or show_custom_error(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			if($row['id'] == 0 || $row['id'] == 1 || $row['id'] == 2 || $row['id'] == 3)
			{
				switch($row['id'])
				{
					case 0:
						$row['name'] = 'Toàn Quốc';
						break;
					case 1:
						$row['name'] = 'Miền Bắc';
						break;
					case 2:
						$row['name'] = 'Miền Trung';
						break;
					case 3:
						$row['name'] = 'Miền Nam';
						break;
				}
			}
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    /*function selfservingGetAllByCampaign($campaignid,$fromdate,$todate,$price,$page,$rownum)
    {
        $data = array();
        $data['banner']=array();
        $data['total']=array();
        $data['totalpage']=array();
        $data['totalprice']=0;
        $total=0;
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_all_by_campaign('$campaignid', '$fromdate', '$todate','$page','$rownum');") or die(mysqli_error());
        
        $result = mysqli_store_result($iconn);
        
        $CI = &get_instance();
        $CI->load->library('mycommon');
        
        while($row = mysqli_fetch_array($result))
		{
            $row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
		 	$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
            switch($row['typead'])
			{
				case '0':
					$row['typead'] = 'CPM';
					break;
                case '1':
					$row['typead'] = 'CPC';
					break;
				default:
					break;
			}
            $row['activate']=date('d/m/Y',strtotime($row['activate']));
            $row['expire']=date('d/m/Y',strtotime($row['expire']));
            $row['price'] = $row['clicks']=='N/A' ? 'N/A' : number_format($row['clicks']*$price);
            $row['uviews'] = $row['uviews']=='N/A' ? 'N/A' : number_format($row['uviews']);
            $row['clicks'] = $row['clicks']=='N/A' ? 'N/A' : number_format($row['clicks']);
            $row['impressions'] = $row['impressions']=='N/A' ? 'N/A' : number_format($row['impressions']);
			$data['banner'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row2 = mysqli_fetch_array($result))
		{
			$data['totalpage'] = $row2[0]['totalpage'];
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
            $row1['totalprice'] = $row1['clicks']=='N/A' ? 'N/A' : number_format($row1['clicks']*$price);
            $row1['clicks'] = $row1['clicks'] =='N/A' ? 'N/A' : number_format($row1['clicks']);
            $row1['impressions'] = $row1['impressions'] == 'N/A' ? 'N/A' : number_format($row1['impressions']);
            $row1['uviews'] = $row1['uviews'] == 'N/A' ? 'N/A' : number_format($row1['uviews']);
			$data['total'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }*/
    
    function selfservingGetListbanners($campaignid)
    {
        $data = array();
        $data['allBanner']=array();
        $data['banner']=array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_listbanners('$campaignid');") or die(mysqli_error());
        $result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['allBanner'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['banner'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfservingGetBannerChart($lstBanner,$fromdate,$todate,$type)
    {
        $data = array();
        $data['banner']=array();
        $data['ban']=array();
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_banner_chart('$lstBanner','$fromdate','$todate','$type');") or die(mysqli_error());
        $result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['banner'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['ban'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfservingGetAllCampaignChart($fromdate, $todate, $lstcampaignid, $type)
    {
        $userid = $this->session->userdata('uid');
        $permitID = $this->session->userdata('permitid');
		$data = array();
        $data['cam1'] = array();
        $data['cam2'] = array();
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_allcampaign_chart('$userid', '$fromdate', '$todate','$permitID', '$lstcampaignid', '$type');") or die(mysqli_error());
        $result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['cam1'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['cam2'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfserving_banner_update_inline($bannerid,$val,$type)
    {
        $userid=$this->session->userdata('uid');
        $data = array();
        $iconn = $this->db->conn_id;
		$result = mysqli_multi_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');");
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $result;
    }
    
    function selfserving_campaign_update_inline($campaignid,$val,$type,$userid)
    {
        $data = array();
        $iconn = $this->db->conn_id;
		$result = mysqli_multi_query($iconn, "CALL selfserving_campaign_update_inline('$campaignid','$val','$type','$userid');") or die(mysqli_error());
		
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		
		return $result;
    }
    
    function selfserving_get_banner_chart_hour($lstbanner,$curdate,$type)
    {
        $data = array();
        $data['bannerinfo']=array();
        $data['bannername']=array();
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_banner_chart_hour('$lstbanner','$curdate','$type');") or die(mysqli_error());
		$result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['bannerinfo'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['bannername'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfserving_get_campaign_chart_hour($campaignid,$curdate,$type,$userid)
    {
        $data = array();
        $data['campaigninfo']=array();
        $data['campaignname']=array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_campaing_chart_hour('$campaignid','$curdate',$type,'$userid');") or die(mysqli_error());
		$result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['campaigninfo'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['campaignname'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfserving_get_user_info($uid)
    {
        $data = array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_user_info('$uid');") or die(mysqli_error());
		$result = mysqli_store_result($iconn);	
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfserving_get_current_value_of_campaign($campaignid,$type)
    {
        $data = array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_current_value_of_campaign('$campaignid','$type');") or die(mysqli_error());
    	$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row['curvalue'];
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
		
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data[] = $row['money'];
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
}
?>